package com.gs.wechatdemo.util;

import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;

import com.gs.wechatdemo.constant.AllConstants;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

    public static final String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";

    public static String DateTimeToStr(Date date) {
        return DateTimeToStr(date, "yyyy-MM-dd");
    }

    public static String DateTimeToStr(Date date, String format) {
        //可根据需要自行截取数据显示
        if (date == null)
            return "";
        SimpleDateFormat simpleDateFormat = null;
        try {
            simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return simpleDateFormat.format(date);
    }

    public static Date StrToDateTime(String str) {
        return StrToDateTime(str, "yyyy-MM-dd");
    }

    public static Date StrToDateTime(String str, String format) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String dateToString(Date date) {
        if (date == null) {
            return "";
        }
        String result;
        long time = date.getTime();
        // 是否为当天
        if (DateUtils.isToday(time)) {
            result = DateUtil.DateTimeToStr(date, "HH:mm");
        } else {
            Calendar nowCalendar = Calendar.getInstance();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            // 是否为当年
            if (nowCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {
                // 是否为本周
                if (calendar.get(Calendar.WEEK_OF_YEAR) == nowCalendar.get(Calendar.WEEK_OF_YEAR)) {
                    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                    int diffDay = dayOfWeek - (nowCalendar.get(Calendar.DAY_OF_WEEK) - 1);
                    // 是否为昨天
                    if (diffDay == -1) {
                        result = "昨天";
                    } else {
                        result = getWeek(dayOfWeek);
                    }
                } else {
                    result = DateUtil.DateTimeToStr(date, "MM月dd日");
                }
            } else {
                result = DateUtil.DateTimeToStr(date, "yyyy年MM月dd日");
            }
        }
        return result;
    }

    public static String dateToStringFromChat(Date date) {
        String result = DateUtil.DateTimeToStr(date, "HH:mm");
        long time = date.getTime();
        // 是否为当天
        if (!DateUtils.isToday(time)) {
            Calendar nowCalendar = Calendar.getInstance();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            // 是否为当年
            if (nowCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {
                // 是否为本周
                if (calendar.get(Calendar.WEEK_OF_YEAR) == nowCalendar.get(Calendar.WEEK_OF_YEAR)) {
                    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                    int diffDay = dayOfWeek - (nowCalendar.get(Calendar.DAY_OF_WEEK) - 1);
                    // 是否为昨天
                    if (diffDay == 1) {
                        result = "昨天 " + result;
                    } else {
                        result = getWeek(dayOfWeek) + " " + result;
                    }
                } else {
                    result = DateUtil.DateTimeToStr(date, "MM月dd日") + " " + result;
                }
            } else {
                result = DateUtil.DateTimeToStr(date, "yyyy年MM月dd日") + " " + result;
            }
        }
        return result;
    }

    private static String getWeek(int day) {
        String result = null;
        switch (day) {
            case 0:
                result = "周日";
                break;
            case 1:
                result = "周一";
                break;
            case 2:
                result = "周二";
                break;
            case 3:
                result = "周三";
                break;
            case 4:
                result = "周四";
                break;
            case 5:
                result = "周五";
                break;
            case 6:
                result = "周六";
                break;
        }
        return result;
    }

    public static boolean isOvertime(Date date){
        if (date == null) {
            return false;
        }
        long time = date.getTime();
        return (System.currentTimeMillis() - time) - (1000 * 60 * 5) >= 0;
    }
}
